//
//  LGAnomalyInspectionController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/11.
//  报告中心 -- 异常指标

#import "LGAnomalyInspectController.h"
//历史回顾
#import "LGInspectChartController.h"
#import "LGReportCenterHeaderView.h"
#import "LGAnomalyInspectCell.h"
@interface LGAnomalyInspectController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIView *topLine;
/**   */
@property (nonatomic , strong)UIView *topView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *patienNum;

@end

@implementation LGAnomalyInspectController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTitleView];
//    [self.view addSubview:self.topLine];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    self.emptyView.backgroundColor = [UIColor clearColor];
}

-(void)viewWillAppearAction{
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Visitrecords/exceptional" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.patienNum = LGNSString(returnData[@"hospital_bano"]);
            self.dataArray = returnData[@"list"];
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            self.topView.hidden = YES;
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:viewPix(30)];
        }else{
            self.topView.hidden = NO;
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            self.topView.hidden = YES;
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:viewPix(30)];
        }else{
            self.topView.hidden = NO;
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *itemArray = self.dataArray[section][@"items"];
    return itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *itemArray = self.dataArray[indexPath.section][@"items"];
    LGAnomalyInspectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(itemArray.count == 1){
        cell.type = @"single";
    }else if (indexPath.row == 0){
        cell.type = @"top";
    }else if (indexPath.row == itemArray.count-1){
        cell.type = @"bottom";
    }else{
        cell.type = @"middle";
    }
    cell.dataDic = itemArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //异常指标趋势
    LGInspectChartController *controller = [[LGInspectChartController alloc]init];
    controller.code = LGNSString(self.dataArray[indexPath.section][@"items"][indexPath.row][@"item_code"]);
    controller.patientNum = self.patienNum;
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGReportCenterHeaderView *headView = [[LGReportCenterHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    headView.content = LGNSString(self.dataArray[section][@"date"]);
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}


#pragma mark
#pragma mark ====> 创建控件
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, viewPix(40), Screen_W, self.view.bounds.size.height-viewPix(40));
    self.emptyView.frame = CGRectMake(0, viewPix(40), Screen_W, self.view.bounds.size.height-viewPix(40));
}

-(UIView *)topLine{
    if(!_topLine){
        _topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
        _topLine.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    }
    return _topLine;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(40), Screen_W, Screen_H-topBarHeight-viewPix(84)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGAnomalyInspectCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = viewPix(46);
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(void)createTitleView{
    CGFloat nameWidth = viewPix(110);
    CGFloat resultWidth = viewPix(70);
    CGFloat itemWidth  = (Screen_W-viewPix(32)-viewPix(16)-viewPix(15)-nameWidth-resultWidth)/2.0;
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(40))];
    baseView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:baseView];
    self.topView = baseView;
    
    UILabel *titleLabel = [UILabel lableWithText:@"项目" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    titleLabel.frame = CGRectMake(viewPix(28), viewPix(13), nameWidth, viewPix(20));
    [baseView addSubview:titleLabel];
    
    UILabel *resultLabel = [UILabel lableWithText:@"结果" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    resultLabel.frame = CGRectMake(kMaxX(titleLabel.frame)+viewPix(5), viewPix(13), resultWidth, viewPix(20));
    [baseView addSubview:resultLabel];
    
    UILabel *unitLabel = [UILabel lableWithText:@"单位" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    unitLabel.frame = CGRectMake(kMaxX(resultLabel.frame)+viewPix(5), viewPix(13), itemWidth, viewPix(20));
    [baseView addSubview:unitLabel];
    
    UILabel *referenceLabel = [UILabel lableWithText:@"参考值" colorString:@"#666666" textFont:LGFont(13) textAlignment:NSTextAlignmentLeft lines:1];
    referenceLabel.frame = CGRectMake(kMaxX(unitLabel.frame)+viewPix(5), viewPix(13), itemWidth, viewPix(20));
    [baseView addSubview:referenceLabel];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
